<%@ page import="org.f0rb.core.web.WebModel" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
 Usage:
<jsp:include page="/WEB-INF/jsp/common/pagebar.jsp">
    <jsp:param name="limit" value="<%=limit%>"/>
    <jsp:param name="offset" value="<%=offset%>"/>
    <jsp:param name="total" value="<%=total%>"/>
</jsp:include>
--%>
<%@ page pageEncoding="UTF-8" %>
<%
    Integer limit = 0, offset;
    Long total;
    WebModel model = (WebModel) request.getAttribute("model");
    if (model != null) {
        limit = model.getLimit();
        offset = model.getOffset();
        total = model.getTotal();
    } else {
        try {
            limit = Integer.valueOf(request.getParameter("limit"));
        } catch (NumberFormatException e) {
        } finally {
            if (limit == 0) limit = 50;
        }
        try {
            offset = Integer.valueOf(request.getParameter("offset"));
        } catch (NumberFormatException e) {
            offset = 0;
        }
        try {
            total = Long.valueOf(request.getParameter("total"));
        } catch (NumberFormatException e) {
            total = 0L;
        }
    }
    Integer p = offset / limit + 1;//current page
    Integer pages = (int) Math.ceil((float) total / limit);

    pageContext.setAttribute("limit", limit);
    pageContext.setAttribute("offset", offset);
    pageContext.setAttribute("total", total);
    pageContext.setAttribute("p", p);
    pageContext.setAttribute("pages", pages);
%>
<jsp:include page="/WEB-INF/jsp/head/jquery.jsp"/>
<script type="text/javascript" src="<c:url value="/resources/lib/jquery/jquery.query.js"/>"></script>
<script type="text/javascript">
    window.page = {
        first:function () {
            location.href = $.query.set('offset', 0).toString();
        },
        last:function () {
            location.href = $.query.set('offset', ${total - total % limit}).toString();
        },
        prev:function () {
            location.href = $.query.set('offset',  ${(offset > limit) ? offset - limit : 0}).toString();
        },
        next:function () {
            location.href = $.query.set('offset', ${offset + limit}).toString();
        },
        size:function (size) { // 设置每页显示条数
            location.href = $.query.set('limit', size).set('offset', ${offset} - ${offset} % size).toString();
        },
        goto:function (p) {
            location.href = $.query.set('offset', (p - 1) * ${limit}).toString();
        }
    };
</script>
<div>
    <span>
        <label>每页<select onchange="page.size(this.value);">
            ${(0 < limit && limit < 10) ? "<option selected>" + limit + "</option>" : ""}
            <c:forEach begin="10" end="50" step="10" var="i">
                <option ${(i == limit) ? "selected" : ""}>${i}</option>
            </c:forEach>
            ${(limit > 50) ? "<option selected>" + limit + "</option>" : ""}
        </select>条</label>
    </span>
    <span>
    <c:choose>
        <c:when test="${offset == 0}">
            <a>|<-</a>
            <a><-</a>
        </c:when>
        <c:otherwise>
            <a href="javascript:page.first();">|<-</a>
            <a href="javascript:page.prev();"><-</a>
        </c:otherwise>
    </c:choose>
    <c:choose>
        <c:when test="${offset + limit >= total}">
            <a>-></a>
            <a>->|</a>
        </c:when>
        <c:otherwise>
            <a href="javascript:page.next();">-></a>
            <a href="javascript:page.last();">->|</a>
        </c:otherwise>
    </c:choose>
    </span>
    <span>
        <label>跳转到第<select onchange="page.goto(this.value);">
            <c:forEach begin="1" end="${pages}" var="i">
                <option ${(i == p) ? "selected" : ""} value=${i}>${i} / ${pages}</option>
            </c:forEach>
        </select>页</label>
    </span>
</div>